package com.yeung.beans;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ConfigurationProperties 注解了 yml配置文件中配置yml-config信息关联(默认从全局配置文件中取值)
 * 可以批量进行值得注入
 * 而   @Value注解只能一个个进行注入  [[只是在业务逻辑中获取配置文件中简单得少量的值是使用@value注解]]
 * <p>
 * 指定取值的资源文件: @PropertySource(value = {"classpath:ymlConfig.properties"})
 * <p>
 * 同时其还支持jsr303校验
 */
@PropertySource(value = {"classpath:ymlConfig.properties"})
@ConfigurationProperties(prefix = "yml-config")
@Component
public class YmlConfig {
//    第二种获取配置文件值得方法
    /**
     * 使用${}从环境变量, 配置文件中获取值,
     * 或者使用#{spel} 表达式获取值
     */
    @Value("${yml-config.name}")
    private String name;
    @Value("#{10*2}")
    private int age;
    @Value("2019/7/20")
    private Date time;
    private List<String> seq;
    private Map<String, String> love;
    private SubConfig sc;


    public YmlConfig() {
    }

    public YmlConfig(String name, int age, Date time, List<String> seq, Map<String, String> love, SubConfig sc) {
        this.name = name;
        this.age = age;
        this.time = time;
        this.seq = seq;
        this.love = love;
        this.sc = sc;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    public List<String> getSeq() {
        return seq;
    }

    public void setSeq(List<String> seq) {
        this.seq = seq;
    }

    public Map<String, String> getLove() {
        return love;
    }

    public void setLove(Map<String, String> love) {
        this.love = love;
    }

    public SubConfig getSc() {
        return sc;
    }

    public void setSc(SubConfig sc) {
        this.sc = sc;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "YmlConfig{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", time=" + time +
                ", seq=" + seq +
                ", love=" + love +
                ", sc=" + sc +
                '}';
    }
}
